package com.mark.vo;

import com.mark.enums.UserStateEnum;
import com.mark.pojo.User;
import com.mark.util.DateTimeUtil;
import com.mark.util.PropertiesUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * Created by Choisaaaa on 2018/5/8.
 * 用户Vo信息的封装
 */

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class UserVo {
    private Integer userId;         //ID
    private String username;        //用户名
    private String email;           //邮箱
    private String phone;           //电话
    private String userImg;         //头像地址
    private String question;        //找密码时的问题
    private String answer;          //回答问题的答案
    private String gender;          //性别
    private Integer enableStatus;   //状态描述【0：可用】 【1：不可用】
    private Integer role;           //身份【1：普通用户】【2：卖家】
    private String roleDes;         //身份描述
    private Integer totalDays;      //成为会员的天数
    private String lastEditTime;    //上一次的修改时间
    private String imageHost;       //图片资源URI

    public UserVo assembleUserVo(User user){
        UserVo userVo = new UserVo();
        userVo.setUserId(user.getId());
        userVo.setUsername(user.getUsername());
        String email = user.getEmail();
        if(StringUtils.isNotBlank(email) && email.contains("@")){
            String[] emails = email.split("@");
            email = emails[0];
            if(StringUtils.isNotBlank(email)&& email.length()>5){
               email = email.substring(0,email.length()-3)+"XXX@"+emails[1];
            }
        }
        userVo.setEmail(email);
        String phone =user.getPhone();
        if(StringUtils.isNotBlank(phone) && phone.length() == 11){
            phone = phone.substring(0,3)+"XXXX"+phone.substring(phone.length()-4,phone.length());
        }
        userVo.setPhone(phone);
        userVo.setUserImg(user.getUserImg());
        userVo.setQuestion(user.getQuestion());
        userVo.setAnswer(user.getAnswer());
        userVo.setGender(user.getGender());
        if(user.getRole() != null){
            String role = assembleStatus(user.getRole());
            userVo.setRoleDes(role);
        }
        userVo.setRole(user.getRole());
        userVo.setEnableStatus(user.getEnableStatus());
        userVo.setLastEditTime(DateTimeUtil.dateToStr(user.getLastEditTime()));
        int dayBetween = DateTimeUtil.dayBetween(user.getCreateTime(),new Date());
        userVo.setTotalDays(dayBetween);
        userVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix",PropertiesUtil.FTP_SERVER_HTTP_PREFIX));
        return userVo;
    }

    private String assembleStatus(Integer enableStatus) {//组装用户身份
        switch (enableStatus){
            case 1 : return "普通用户";
            case 2 : return "卖家";
            default: return "不明确";
        }
    }
}
